package org.millioncall.yueyoga.admin.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.millioncall.openplatform.common.PaginatedList;
import org.millioncall.openplatform.util.WebAppUtils;
import org.millioncall.yueyoga.admin.model.Groups;
import org.millioncall.yueyoga.admin.model.Member;
import org.millioncall.yueyoga.admin.model.MemberGroup;
import org.millioncall.yueyoga.admin.service.GroupsService;
import org.millioncall.yueyoga.admin.service.MemberGroupService;
import org.millioncall.yueyoga.admin.service.MemberService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author lisj 会员组 2014年5月3日 上午9:09:15
 */
@Controller
public class MemberGroupAction extends BaseAction {

	private GroupsService groupsService;
	private MemberService memberService;
	private MemberGroupService memberGroupService;

	// 转向登录页
	@RequestMapping(value = "/findMemberGroups.jhtml")
	public String findMemberGroups(String groupName, String beginInputTime,
			String endInputTime, String pageNo, String pageSize,
			Map<String, Object> map, ModelMap modelMap,
			HttpServletRequest request, HttpServletResponse response) {
		this.initFrontHeader(request, modelMap);
		map.put("hallCode", this.getUser(request).getOrganizationId());

		int start = 0;
		int limit = 10;

		if (StringUtils.isNotBlank(pageSize)) {
			limit = Integer.parseInt(pageSize);
		}

		if (StringUtils.isNotBlank(pageNo)) {
			start = (Integer.parseInt(pageNo) - 1) * limit;
		}

		if (StringUtils.isNotBlank(groupName)) {
			map.put("groupName", groupName);
		}
		if (StringUtils.isNotBlank(beginInputTime)) {
			map.put("beginInputTime", beginInputTime);
		}
		if (StringUtils.isNotBlank(endInputTime)) {
			map.put("endInputTime", endInputTime);
		}
		modelMap.put("memberGroups",
				groupsService.findMemberGroupsByCdts(map, start, limit));
		return "member_group_search";
	}

	// 进入添加页面
	@RequestMapping(value = "/toAddMemberGroup.jhtml")
	public String toAddMemberGroup(ModelMap model, HttpServletRequest request,
			HttpServletResponse response) {
		this.initFrontHeader(request, model);
		return "member_group_add";
	}

	// 进入编辑页面
	@RequestMapping(value = "/toEditMemberGroup.jhtml")
	public String toEditMemberGroup(String groupId, ModelMap model,
			HttpServletRequest request, HttpServletResponse response) {
		this.initFrontHeader(request, model);
		Groups group = new Groups();
		if (StringUtils.isNotBlank(groupId)) {
			group = groupsService.findGroupsById(Integer.valueOf(groupId));
		}
		model.put("group", group);
		
		int start = 0;
		int limit = 10;
		String pageNo = request.getParameter("pageNo");
		String pageSize = request.getParameter("pageSize");
		if (StringUtils.isNotBlank(pageSize)) {
			limit = Integer.parseInt(pageSize);
		}
		if (StringUtils.isNotBlank(pageNo)) {
			start = (Integer.parseInt(pageNo) - 1) * limit;
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("groupId", group.getId());
		map.put("hallcode", this.getUser(request).getOrganizationId());
		List<Member> members = this.memberService.findAllMember(map);
		PaginatedList<Member> membersGroup = this.memberService.findAllMemberByGroup(start, limit, group.getId());
		model.put("members", members);
		model.put("membersGroup", membersGroup);
		return "member_group_edit";
	}

	// 添加或修改
	@RequestMapping(value = "/updateMemberGroup.jhtml")
	public String updateMemberGroup(Groups group, ModelMap model,
			HttpServletRequest request, HttpServletResponse response) {
		this.initFrontHeader(request, model);
		String msg = null;
		if (group != null && group.getId() != null) {
			if (groupsService.updateGroupsById(group)) {
				msg = "ets";
			} else {
				msg = "ete";
			}
		} else {
			group.setHallCode(this.getUser(request).getOrganizationId());
			if (groupsService.saveGroups(group)) {
				msg = "ads";
			} else {
				msg = "ade";
			}
		}
		model.put("msg", msg);
		return "redirect:findMemberGroups.jhtml";
	}

	// 删除
	@RequestMapping(value = "/deleteMemberGroupsByIds.jhtml")
	public void deleteMemberGroupsByIds(String ids, ModelMap model,
			HttpServletRequest request, HttpServletResponse response) {
		this.initFrontHeader(request, model);
		String msg = null;
		String[] groupIds = null;
		if (StringUtils.isNotBlank(ids) && !",".equals(ids)) {
			groupIds = ids.split(",");
		}
		if (groupsService.deleteGroupsByIds(groupIds)) {
			msg = "true";
		} else {
			msg = "false";
		}
		WebAppUtils.ajaxResponse(response, msg);
	}
	
	@RequestMapping(value="addGroupMember.jhtml")
	public void addGroupMember(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
		String strMemberId = request.getParameter("memberId");
		String strGroupId = request.getParameter("groupId");
		String msg = "";
		Integer memberId = 0, groupId = 0;
		
		try {
			memberId = Integer.parseInt(strMemberId);
			groupId = Integer.parseInt(strGroupId);
		} catch (NumberFormatException e) {
			msg = "{'success':false, 'msg':'会员编号或组编号格式不正确！'}";
			WebAppUtils.ajaxResponse(response, msg);
			return;
		}
		
		MemberGroup mg = new MemberGroup();
		mg.setGroupid(groupId);
		mg.setMemberid(memberId);
		
		int i = this.memberGroupService.insert(mg);
		if(i > 0) {
			msg = "{'success':true, 'msg':'保存组会员成功！'}";
		} else {
			msg = "{'success':false, 'msg':'保存组会员失败！'}";
		}
		
		WebAppUtils.ajaxResponse(response, msg);
	}
	
	/** 从组中删除会员 */
	@RequestMapping(value="deleteGroupMember.jhtml")
	public void deleteGroupMember(HttpServletRequest request, HttpServletResponse response) {
		String strMemberId = request.getParameter("memberId");
		String strGroupId = request.getParameter("groupId");
		String msg = "";
		Integer memberId = 0, groupId = 0;
		
		try {
			memberId = Integer.parseInt(strMemberId);
			groupId = Integer.parseInt(strGroupId);
		} catch (NumberFormatException e) {
			msg = "{'success':false, 'msg':'会员编号或组编号格式不正确！'}";
			WebAppUtils.ajaxResponse(response, msg);
			return;
		}
		
		MemberGroup mg = new MemberGroup();
		mg.setGroupid(groupId);
		mg.setMemberid(memberId);
		
		int i = this.memberGroupService.delete(mg);
		if(i > 0) {
			msg = "{'success':true, 'msg':'删除组会员成功！'}";
		} else {
			msg = "{'success':false, 'msg':'删除组会员失败！'}";
		}
		
		WebAppUtils.ajaxResponse(response, msg);
	}
	
	/** 查找最会员个数 */
	@RequestMapping(value="getGroupMemberCount.jhtml")
	public void findGroupMember(HttpServletRequest request, HttpServletResponse response) {
		String strMemberId = request.getParameter("memberId");
		String strGroupId = request.getParameter("groupId");
		String msg = "";
		Integer memberId = 0, groupId = 0;
		
		try {
			memberId = Integer.parseInt(strMemberId);
			groupId = Integer.parseInt(strGroupId);
		} catch (NumberFormatException e) {
			msg = "{'success':false, 'msg':'会员编号或组编号格式不正确！'}";
			WebAppUtils.ajaxResponse(response, msg);
			return;
		}
		
		MemberGroup mg = new MemberGroup();
		mg.setGroupid(groupId);
		mg.setMemberid(memberId);
		int count = this.memberGroupService.getGroupMemberCount(mg);
		
		if(count > 0) {
			msg = "{'success':true, 'msg':'查找组会员成功！'}";
		} else {
			msg = "{'success':false, 'msg':'查找组会员失败！'}";
		}
		
		WebAppUtils.ajaxResponse(response, msg);
	}

	public GroupsService getGroupsService() {
		return groupsService;
	}

	public void setGroupsService(GroupsService groupsService) {
		this.groupsService = groupsService;
	}

	public MemberService getMemberService() {
		return memberService;
	}

	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}

	public MemberGroupService getMemberGroupService() {
		return memberGroupService;
	}

	public void setMemberGroupService(MemberGroupService memberGroupService) {
		this.memberGroupService = memberGroupService;
	}
}
